<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="user_id" label="用户ID" required>
        <uni-easyinput v-model="formData.user_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cell_id" label="寄存格ID" required>
        <uni-easyinput v-model="formData.cell_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="start_time" label="开始时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.start_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="end_time" label="结束时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.end_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="actual_end_time" label="实际结束时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.actual_end_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="total_price" label="总金额">
        <uni-easyinput type="number" v-model="formData.total_price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="size_type" label="格子尺寸类型">
        <uni-data-checkbox v-model="formData.size_type" :localdata="formOptions.size_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="status" label="订单状态">
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="payment_status" label="支付状态">
        <uni-data-checkbox v-model="formData.payment_status" :localdata="formOptions.payment_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="创建时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/mydb-order-list.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mydb-order-list';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "user_id": "",
        "cell_id": "",
        "start_time": null,
        "end_time": null,
        "actual_end_time": null,
        "total_price": null,
        "size_type": null,
        "status": 0,
        "payment_status": 0,
        "coupon_id": "",
        "package_id": "",
        "create_time": null,
        "update_time": null
      }
      return {
        formData,
        formOptions: {
          "size_type_localdata": [
            {
              "value": 0,
              "text": '小'
            },
            {
              "value": 1,
              "text": '中'
            },
            {
              "value": 2,
              "text": '大'
            }
          ],
          "status_localdata": [
            {
              "value": 0,
              "text": '待支付'
            },
            {
              "value": 1,
              "text": '已支付'
            },
            {
              "value": 2,
              "text": '已完成'
            },
            {
              "value": 3,
              "text": '已退款'
            },
            {
              "value": 4,
              "text": '已取消'
            }
          ],
          "payment_status_localdata": [
            {
              "value": 0,
              "text": '待支付'
            },
            {
              "value": 1,
              "text": '已支付'
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("user_id,cell_id,start_time,end_time,actual_end_time,total_price,size_type,status,payment_status,coupon_id,package_id,create_time,update_time").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
